#!/usr/bin/python
# -*- coding: utf-8 -*-
"""用户相关rpc"""
from jiaju.common.jsonrpcserver import jsonrpc_function
from jiaju.business import user_manager, privilege_manager, type_manager
from jiaju.business import page_manager
from jiaju.common.decorators import rpc_privilege_required
from jiaju.common.constants import privilege_constant

"""用管管理操作 start """
@jsonrpc_function
@rpc_privilege_required(page_id=1, operator_id=privilege_constant.Privileges.ACTION_TYPE_ADD)
def add_user(request, user_info):
    return user_manager.add_user(user_info)

@jsonrpc_function
@rpc_privilege_required(page_id=1, operator_id=privilege_constant.Privileges.ACTION_TYPE_UPDATE)
def edit_user(request, user_info):
    return user_manager.add_user(user_info)

@jsonrpc_function
@rpc_privilege_required(page_id=1, operator_id=privilege_constant.Privileges.ACTION_TYPE_DELETE)
def remove_user(request , id):
    return user_manager.del_user(id)
"""用管管理操作 end """

"""权限操作 start"""
@jsonrpc_function
@rpc_privilege_required(page_id=4, operator_id=privilege_constant.Privileges.ACTION_TYPE_UPDATE)
def set_privilege(reuqest , infos):
    if infos.get("user_id") and infos.get("page_id") :
        info_one = privilege_manager.get_privilege_list(infos.get("user_id"), infos.get("page_id"))
        privilege_value_temp = infos.get("privilege_value")
        id_temp=0
        if info_one and info_one.id:
            id_temp = info_one.id
        if not privilege_value_temp:
            privilege_value_temp = 1
            
        privilege_manager.meger_privilege({'privilege_value':privilege_value_temp,\
                                           'user_id':infos.get("user_id"),\
                                           'page_id':infos.get("page_id"),\
                                           'id':id_temp})
    else:
        raise "user_id or page_id is null"
"""权限操作 end"""

"""页面管理操作 start"""
@jsonrpc_function
@rpc_privilege_required(page_id=2, operator_id=privilege_constant.Privileges.ACTION_TYPE_ADD)
def add_page(request , page_info):
    return page_manager.merge_page_info(page_info)

@jsonrpc_function
@rpc_privilege_required(page_id=2, operator_id=privilege_constant.Privileges.ACTION_TYPE_UPDATE)
def edit_page(request , page_info):
    return page_manager.merge_page_info(page_info)

@jsonrpc_function
@rpc_privilege_required(page_id=2, operator_id=privilege_constant.Privileges.ACTION_TYPE_DELETE)
def delete_page(request , page_info):
    return page_manager.merge_page_info(page_info)
"""页面管理操作 end"""

@jsonrpc_function
def add_type(request,typeinfo):
    return type_manager.add_type(typeinfo)

@jsonrpc_function
def delete_type(request,id):
    return type_manager.delete_type(id)